<?php
/**
 * @file bg-cc.php	Fixed!
 * @version 0.1
 * @desc 得到订单列表
 * 
 */
$action = $_POST['action'];
$user_id = $_POST['user_id'];
$bid = $_POST['bid'];
$type = $_POST['type'];
$offset = $_POST['offset'];

$msg = 0;
/**
 * 1. 流水操作成功
 * 2. 没有流水操作
 * 
 * 444. 数据库连接失败
 * 
 * 410. action为空 或者 错误
 * 411. 用户为空
 * 413. 操作数offset为空
 * 414. 操作数type为空
 * 415. type错误
 * 416. bid委托号为空
 * 
 */
if (empty($action)){
	$msg = 410;
}elseif (empty($user_id)){
	$msg = 411;
}elseif (empty($type)){
	$msg = 414;
}elseif (!isset($offset)){
	$msg = 413;
}elseif (!isset($bid)){
	$msg = 416;
}

$list = array();
$sum = 0;
if($msg == 0){
	require_once 'setting.php';
	
	if ($type == '1') {  //委托查询
		$query = "SELECT count(`bus_id`) AS knum FROM `bus_trust` WHERE `user_id` = {$user_id}";
	}elseif ($type == '2') {
		$query = "SELECT COUNT(`pay_id`) AS knum FROM `bus_deal` 
				WHERE `user_id` = {$user_id} AND `bus_id` = {$bid}";
	}elseif ($type == '3'){
		$query = "SELECT COUNT(`pay_id`) AS knum FROM `bus_deal2` 
				WHERE `user_id` = {$user_id} AND `kbao_num` > 0";
	}elseif ($type == '4'){
		$query = "SELECT COUNT(`pay_id`) AS knum FROM `bus_deal2` 
				WHERE `user_id` = {$user_id} AND `kbi_num` > 0";
	}else $msg = 415;
	
	if ($msg == 0) {
		$re2 = Appleseed::executeSQL($query);
		if (!$re2) $msg = 2;   //没有委托过
		else {
			$sum = $re2['knum'];
		}
	}
	
	if ($msg == 0) {
		$re = Appleseed::getTrustList($user_id, $bid, $type, $offset);
		foreach($re as $t){
			if ($type == '1'){
				array_push($list,array('bus_id' => $t['bus_id'],
							'price' => $t['price'], 'time' => $t['time'],
							'num' => $t['num'], 'num_left' => $t['num_left'],
							'status' => $t['status'], 'flag' => $t['flag'] )
					);
			}elseif ($type == '2'){
				array_push($list, array('pay_id' => $t['pay_id'], 'bus_id' => $t['bus_id'],
							'flag' => $t['flag'], 'num' => $t['num'], 
							'price' => $t['price'], 'time' => $t['time'],
							'sum' => $t['sum'] )
				);
			}elseif ($type == '3'){
				array_push($list, array('pay_id' => $t['pay_id'], 'bus_id' => $t['bus_id'],
							'flag' => $t['flag'], 'time' => $t['time'],
							'kbao_num' => $t['kbao_num'], 'kbao_left' => $t['kbao_left'],
							'parent_id' => $t['parent_id'] )
				);
			}elseif ($type == '4'){
				array_push($list, array('pay_id' => $t['pay_id'], 'bus_id' => $t['bus_id'],
							'flag' => $t['flag'], 'time' => $t['time'],
							'kbi_num' => $t['kbi_num'], 'kbi_left' => $t['kbi_left'],
							'parent_id' => $t['parent_id'] )
				);				
			} // ---
		}
	}
	if (empty($list)){
		$msg = 2;
	}else $msg = 1;
}
$result = array('msg' => $msg,
				'sum' => $sum,
				'list' => $list);
echo json_encode($result);
?>